package grape.auth.rest.clientdetails.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.RBaseException;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.auth.rest.clientdetails.form.OauthClientDetailsCreateForm;
import grape.auth.rest.clientdetails.form.OauthClientDetailsUpdateForm;
import grape.auth.rest.clientdetails.form.OauthClientDetailsListPageForm;
import grape.auth.rest.clientdetails.vo.OauthClientDetailsVo;
import grape.auth.rest.clientdetails.mapper.OauthClientDetailsWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.auth.service.clientdetails.po.OauthClientDetails;
import grape.auth.service.clientdetails.api.IOauthClientDetailsService;
import java.util.List;
/**
 * <p>
 * 服务客户端表 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-03-31
 */
@RestController
@RequestMapping("/auth/oauthclientdetails")
@Api(tags = "服务客户端相关接口")
public class OauthClientDetailsController extends BaseController<OauthClientDetailsVo, OauthClientDetails> {

    @Autowired
    private OauthClientDetailsWebMapper currentWebMapper;
    @Autowired
    private IOauthClientDetailsService currentService;

    @Autowired
    private PasswordEncoder passwordEncoder;


     @ApiOperation("添加服务客户端")
     @PreAuthorize("hasAuthority('oauthclientdetails:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public OauthClientDetailsVo create(@RequestBody @Valid OauthClientDetailsCreateForm cf) {
         // code 唯一检查
         if (currentService.existClientId(cf.getClientId())) {
             throw new RBaseException("clientId已存在");
         }
         OauthClientDetails po = currentWebMapper.formToPo(cf);
         po.setClientSecret(passwordEncoder.encode(cf.getClientSecret()));
         return super.create(po);
     }

     @ApiOperation("根据ID查询服务客户端")
     @PreAuthorize("hasAuthority('oauthclientdetails:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public OauthClientDetailsVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除服务客户端")
     @PreAuthorize("hasAuthority('oauthclientdetails:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新服务客户端")
     @PreAuthorize("hasAuthority('oauthclientdetails:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public OauthClientDetailsVo update(@PathVariable String id,@RequestBody @Valid OauthClientDetailsUpdateForm uf) {

         // clientId要唯一
         OauthClientDetails byClientId = currentService.getByClientId(uf.getClientId());
         if (!isEqual(id, byClientId.getId())) {
             throw new RBaseException("clientId已存在");
         }
         OauthClientDetails po = currentWebMapper.formToPo(uf);
         po.setId(id);
         if(!isStrEmpty(po.getClientSecret())){
             po.setClientSecret(passwordEncoder.encode(po.getClientSecret()));
         }
         return super.update(po);
     }

    @ApiOperation("分页查询服务客户端")
    @PreAuthorize("hasAuthority('oauthclientdetails:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<OauthClientDetailsVo> listPage(OauthClientDetailsListPageForm listPageForm) {
         OauthClientDetails po = currentWebMapper.formToPo(listPageForm);
         return super.listPage(po,listPageForm);
     }

    @Override
    public OauthClientDetailsVo transVo(OauthClientDetailsVo oauthClientDetailsVo) {
         // 擦除密码
        oauthClientDetailsVo.setClientSecret(null);
        return super.transVo(oauthClientDetailsVo);
    }
}
